#include <event2/event.h>
#include <event2/event_struct.h>

class LibEventDispatcherWithTimer : public LibEventDispatcher
{
private:
    struct event timer_event_;
    struct timeval timer_interval_;

public:
    LibEventDispatcherWithTimer() {
        // 初始化定时器事件
        evtimer_assign(&timer_event_, get_event_base(), timer_callback, this);
        timer_interval_.tv_sec = 1;   // 1秒
        timer_interval_.tv_usec = 0;
        
        // 启动定时器
        evtimer_add(&timer_event_, &timer_interval_);
    }
    
    ~LibEventDispatcherWithTimer() {
        event_del(&timer_event_);
    }
    
    struct event_base* get_event_base() const {
        return static_cast<LibEventDispatcher*>(const_cast<LibEventDispatcherWithTimer*>(this))->base_;
    }

private:
    static void timer_callback(evutil_socket_t fd, short events, void* arg) {
        LibEventDispatcherWithTimer* dispatcher = static_cast<LibEventDispatcherWithTimer*>(arg);
        
        std::cout << "Timer tick: " << std::time(nullptr) << std::endl;
        
        // 重新调度定时器
        evtimer_add(&dispatcher->timer_event_, &dispatcher->timer_interval_);
    }
};